package com.zsy.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zsy.dao.CategoryDao;
import com.zsy.dao.DishDao;
import com.zsy.dao.DishFlavorDao;
import com.zsy.dto.DishDto;
import com.zsy.exception.CustomException;
import com.zsy.pojo.*;
import com.zsy.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service

@Slf4j
public class DiahServiceImpl implements DishService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private DishDao dishDao;

    @Autowired(required = false)
    private DishFlavorDao dishFlavorDao;

    @Autowired
    private CategoryDao categoryDao;

    @Override
    @Transactional
    public void save(DishDto dishDto) {
        dishDto.setCreateTime(LocalDateTime.now());
        dishDto.setUpdateTime(LocalDateTime.now());
        dishDto.setSort(0);
        dishDto.setStatus(1);

        dishDao.save(dishDto);

        List<DishFlavor> flavors = dishDto.getFlavors();

        List<DishFlavor> list = flavors.stream().map((flavor -> {
            flavor.setDishId(dishDto.getId());
            flavor.setCreateTime(LocalDateTime.now());
            flavor.setUpdateTime(LocalDateTime.now());
            flavor.setCreateUser(dishDto.getCreateUser());
            flavor.setUpdateUser(dishDto.getUpdateUser());
            return flavor;
        })).collect(Collectors.toList());

        dishFlavorDao.save(list);

        redisTemplate.delete(redisTemplate.keys("dish_*"));


    }

    @Override
    @Transactional
    public Page<DishDto> page(int size, int sizePage, String name) {

        PageHelper.startPage(size, sizePage);

        List<Dish> dishList = dishDao.findByName(name);

        PageInfo<Dish> pageInfo = new PageInfo<>(dishList);


        List<DishDto> collect = pageInfo.getList().stream().map(dish -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            Category byId = categoryDao.findById(dish.getCategoryId());
            dishDto.setCategoryName(byId.getName());
            return dishDto;
        }).collect(Collectors.toList());

        return new Page<>(collect, pageInfo.getTotal(), pageInfo.getSize(), pageInfo.getPageSize());

    }

    @Override
    @Transactional
    public DishDto findById(Long id) {
        //根据前端传来的id获取dish数据
        Dish dish = dishDao.findId(id);
        //查询口味数组
        List<DishFlavor> dishFlavors = dishFlavorDao.findById(id);
        //new 出向前端展示的Dto
        DishDto dishDto = new DishDto();
        //将dish复制给dishDto
        BeanUtils.copyProperties(dish, dishDto);
        //设置dishDto的口味数组
        dishDto.setFlavors(dishFlavors);
        //返回dishDish
        return dishDto;
    }

    @Override
    @Transactional
    public void update(DishDto dishDto) {
        dishDto.setUpdateTime(LocalDateTime.now());

        dishDao.updateById(dishDto);

        dishFlavorDao.deleteById(dishDto.getId());

        List<DishFlavor> flavors = dishDto.getFlavors();

        List<DishFlavor> collect = flavors.stream().map(flavor -> {
            flavor.setDishId(dishDto.getId());
            flavor.setUpdateTime(LocalDateTime.now());
            flavor.setCreateTime(LocalDateTime.now());
            flavor.setUpdateUser(dishDto.getUpdateUser());
            flavor.setCreateUser(dishDto.getCreateUser());
            return flavor;
        }).collect(Collectors.toList());

        dishFlavorDao.save(collect);

        redisTemplate.delete(redisTemplate.keys("dish_*"));
    }

    @Override
    public List<DishDto> dishList(Long categoryId, Integer status) {
        List<DishDto> dishDtoList = null;

        ValueOperations valueOperations = redisTemplate.opsForValue();
        String key = "dish_" + categoryId + "_" + status;
        dishDtoList = (List<DishDto>) valueOperations.get(key);

        if (dishDtoList == null) {
            List<Dish> dishLists = dishDao.dishList(categoryId, status);

            dishDtoList = dishLists.stream().map(dish -> {
                //Dish复制进DishDto
                DishDto dishDto = new DishDto();
                BeanUtils.copyProperties(dish, dishDto);
                //dishFlavors赋值进DishDto
                List<DishFlavor> dishFlavors = dishFlavorDao.findById(dish.getId());
                dishDto.setFlavors(dishFlavors);
                //获取菜品名赋值进DishDto
                Category category = categoryDao.findById(dish.getCategoryId());
                dishDto.setCategoryName(category.getName());

                return dishDto;
            }).collect(Collectors.toList());

            valueOperations.set(key,dishDtoList,60*24, TimeUnit.MINUTES);
        }
        return dishDtoList;

    }

    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        dishDao.updateStatus(status, ids);
    }

    @Override
    public void delete(List<Long> ids) {
        Integer i = dishDao.findByIds(ids);
        if (i == 0) {
            dishDao.delete(ids);
        } else {
            throw new CustomException("该菜品处于起售状态，无法删除");
        }
    }
}

